/**
 */
package com.dingpeikeji.quickdeer.core.modules.cupons.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.cupons.dao.TCuponsDao;
import com.dingpeikeji.quickdeer.core.modules.cupons.entity.CuponsInfo;
import com.dingpeikeji.quickdeer.core.modules.cupons.service.TCuponsService;
import com.dingpeikeji.quickdeer.core.modules.driver.entity.TDrivers;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.line.service.TLineService;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;


/**
 * 优惠券Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/cupons/tCupons")
public class TCuponsController extends BaseController {

	@Autowired
	private TCuponsService tCuponsService;
	
	@ModelAttribute
	public CuponsInfo get(@RequestParam(required=false) String cuponsid) {
		
		if (StringUtils.isNotBlank(cuponsid)){
			return tCuponsService.get(cuponsid);
		}else{
			return new CuponsInfo();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(CuponsInfo tCuponsInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CuponsInfo> page= tCuponsService.findPage(new Page<CuponsInfo>(request, response), tCuponsInfo); 
		if(tCuponsInfo.getState()!=null)
		if(tCuponsInfo.getState().equals("3")){
			for(int i=0;i<page.getList().size();i++){
				if(page.getList()!=null){
				if(page.getList().get(i).getType().equals("0"))	{
					if(page.getList().get(i).getIsDisable().equals("1")||page.getList().get(i).getTimeOver().getTime()<=(new Date()).getTime()){
						page.getList().remove(i);
						i--;
					}
				}else{
					if(page.getList().get(i).getIsDisable().equals("1")){
						page.getList().remove(i);
						i--;
					}
				}
			 }
			}
		}else if(tCuponsInfo.getState().equals("2")){
			for(int i=0;i<page.getList().size();i++){
				if(page.getList()!=null){
					
				if(page.getList().get(i).getType().equals("0"))	{
					if(page.getList().get(i).getIsDisable().equals("0")||page.getList().get(i).getTimeOver().getTime()<(new Date()).getTime()||page.getList().get(i).getTimeBegin().getTime()>(new Date()).getTime()){
						page.getList().remove(i);
						i--;
					}
				}else{
					if(page.getList().get(i).getIsDisable().equals("0")){
						page.getList().remove(i);
						i--;
					}
				}
			 }
			}
		}else if(tCuponsInfo.getState().equals("1")){
			for(int i=0;i<page.getList().size();i++){
				if(page.getList()!=null){
				if(!page.getList().get(i).getType().equals("0")||page.getList().get(i).getIsDisable().equals("0")||page.getList().get(i).getTimeBegin().getTime()<=(new Date()).getTime()){
					page.getList().remove(i);
					i--;
				}
			}
			}
		}
		
		for(int i=0;i<page.getList().size();i++){
			page.getList().get(i).setPickNum(tCuponsService.getPickInfo(page.getList().get(i).getCuponsId()).size()+"");
			page.getList().get(i).setUseNum(tCuponsService.getUseInfo(page.getList().get(i).getCuponsId()).size()+"");
		}
		model.addAttribute("page", page);
		return "modules/cupons/tCuponsList";
	}

	@RequestMapping(value = "form")
	public String form(CuponsInfo tCuponsInfo, Model model) {
		if(tCuponsInfo.getCuponsId()!=null){
			tCuponsInfo=tCuponsService.get(tCuponsInfo.getCuponsId());
		}
		model.addAttribute("tCuponsInfo", tCuponsInfo);
		return "modules/cupons/tCuponsForm";
	}
	
	@RequestMapping(value = "add")
	public String add(CuponsInfo tCuponsInfo, Model model) {
		model.addAttribute("tCuponsInfo", tCuponsInfo);
		return "modules/cupons/tCuponsAdd";
	}
	
	@RequestMapping(value = "save")
	public String save(CuponsInfo tCuponsInfo, HttpServletRequest request, HttpServletResponse response,Model model, RedirectAttributes redirectAttributes) {
		int j=0;
		if(tCuponsInfo!=null){
			if(tCuponsInfo.getCuponsId()!=null&&tCuponsInfo.getCuponsId().length()>0){
				tCuponsInfo.setIsDisable("1");
				tCuponsInfo.setCreateTime(new Date());
				j=tCuponsService.updateCupons(tCuponsInfo);
			}else{
				tCuponsInfo.setCuponsId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
				tCuponsInfo.setIsDisable("1");
				tCuponsInfo.setCreateTime(new Date());
				j = tCuponsService.insertCupons(tCuponsInfo);
			}
		}
			Page<CuponsInfo> page = tCuponsService.findPage(new Page<CuponsInfo>(request, response), tCuponsInfo); 
			for(int i=0;i<page.getList().size();i++){
				page.getList().get(i).setPickNum(tCuponsService.getPickInfo(page.getList().get(i).getCuponsId()).size()+"");
				page.getList().get(i).setUseNum(tCuponsService.getUseInfo(page.getList().get(i).getCuponsId()).size()+"");
			}
			model.addAttribute("page", page);
			return "modules/cupons/tCuponsList";
	}
	
	@RequestMapping(value = "drop")
	public String drop(CuponsInfo tCuponsInfo, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {
		
		tCuponsInfo = tCuponsService.get(tCuponsInfo.getCuponsId());
		if(tCuponsInfo!=null){
			if(tCuponsInfo.getIsDisable().equals("1")){
				tCuponsInfo.setIsDisable("0");
			}
			int i = tCuponsService.updateCupons(tCuponsInfo);
		}
		
		Page<CuponsInfo> page = tCuponsService.findPage(new Page<CuponsInfo>(request, response), tCuponsInfo); 
		for(int i=0;i<page.getList().size();i++){
			page.getList().get(i).setPickNum(tCuponsService.getPickInfo(page.getList().get(i).getCuponsId()).size()+"");
			page.getList().get(i).setUseNum(tCuponsService.getUseInfo(page.getList().get(i).getCuponsId()).size()+"");
		}
		model.addAttribute("page", page);
		return "modules/cupons/tCuponsList";
	}
	
	@RequestMapping(value = "pick")
	public String pick(CuponsInfo tCuponsInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CuponsInfo> page = new Page<CuponsInfo>(request, response); 
		page.setList(tCuponsService.getPickInfo(tCuponsInfo.getCuponsId()));
		model.addAttribute("page", page);
		return "modules/cupons/tCuponsPickList";
	}
	
	@RequestMapping(value = "use")
	public String use(CuponsInfo tCuponsInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CuponsInfo> page = new Page<CuponsInfo>(request, response); 
		page.setList(tCuponsService.getUseInfo(tCuponsInfo.getCuponsId()));
		model.addAttribute("page", page);
		model.addAttribute("page", page);
		return "modules/cupons/tCuponsUseList";
	}
	
//	@RequiresPermissions("line:tLine:edit")
//	@RequestMapping(value = "delete")
//	public String delete(TLine tLine, RedirectAttributes redirectAttributes) {
//		tLineService.delete(tLine);
//		addMessage(redirectAttributes, "删除线路定义成功");
//		return "redirect:"+Global.getAdminPath()+"/line/tLine/?repage";
//	}
//
//	@ResponseBody
//	@RequiresPermissions("line:tLine:edit")
//	@RequestMapping(value = "checklinename")
//	public String checkLineName(String oldlinename, String linename) {
//		if (linename !=null && linename.equals(oldlinename)) {
//			return "true";
//		} else if (linename !=null && tLineService.getLineByName(linename) == null) {
//			return "true";
//		}
//		return "false";
//	}

}
